#!/bin/bash
tmp_root_dir=$PWD
source ~/.profile

echo "==============================================================="
echo -e "      \e[41mOne Key Deploy MicroPython Development for IDF v4.2\e[0m"
echo "==============================================================="

update_system() {
	echo ""
	echo -e "\e[32mUpdate system...\e[0m"
	sudo apt-get update
	sudo apt-get install -y git

	git config --global credential.helper store
}

clone_idf_from_gitee() {
	echo ""
	echo -e "\e[32mClone IDF v4.2 project from gitee...\e[0m"
	cd $tmp_root_dir
	git clone -b release/v4.2 https://gitee.com/EspressifSystems/esp-idf.git
	echo "export IDF_PATH=$tmp_root_dir/esp-idf" >> ~/.profile
	source ~/.profile
}

clone_egt_from_gitee() {
	echo ""
	echo -e "\e[32mClone ESP Gitee Tools project from gitee...\e[0m"
	cd $tmp_root_dir
	git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git
}

update_egt() {
	echo ""
	echo -e "\e[32mUpdating ESP Gitee Tools...\e[0m"
	cd $tmp_root_dir/esp-gitee-tools
	git pull
}

update_idf() {
	echo ""
	echo -e "\e[32mUpdating IDF...\e[0m"
	cd $IDF_PATH
	git pull
}

update_idf_submodules() {
	echo ""
	echo -e "\e[32mUpdating IDF submodules...\e[0m"
	$tmp_root_dir/esp-gitee-tools/submodule-update.sh $IDF_PATH
}

install_development_support() {
	echo ""
	echo -e "\e[32mInstalling development...\e[0m"
	sudo apt-get install -y wget flex bison gperf python3 python3-pip python3-setuptools libpython3-dev libpython-dev cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
	sudo apt-get install -y gcc make libncurses-dev python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing python-pyelftools tar

	sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7   1
	sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6   2
}

install_idf_toolchains() {
	echo ""
	echo -e "\e[32mInstalling toolchains...\e[0m"
	$tmp_root_dir/esp-gitee-tools/install.sh
	# pip install click
}

clone_micropython_from_gitee() {
	echo ""
	echo -e "\e[32mClone MicroPython project from gitee...\e[0m"
	cd $tmp_root_dir
	git clone https://gitee.com/WalklineStudio/micropython.git
}

update_micropython() {
	echo ""
	echo -e "\e[32mUpdating MicroPython...\e[0m"
	cd $tmp_root_dir/micropython
	git fetch --all && git reset --hard origin/master
	git pull
}

update_micropython_submodules() {
	echo ""
	echo -e "\e[32mUpdating MicroPython submodules...\e[0m"
	cd $tmp_root_dir
	./submodule-update.sh micropython
	# ./submodule-update.sh micropython/lib/pico-sdk
}

compile_firmware() {
	echo ""
	echo -e "\e[32mCompiling firmware...\e[0m"
	# echo "export Python3_DIR=~/.espressif/python_env/idf4.2_py3.6_env" >> ~/.profile
	# echo "export Python3_DIR=/usr/bin" >> ~/.profile
	# source ~/.profile
	source $IDF_PATH/export.sh
	# source $Python3_DIR/bin/activate
	# pip3 install -r $IDF_PATH/requirements.txt

	cd $tmp_root_dir
	cd micropython/mpy-cross && make -j3 && cd $tmp_root_dir
	cd micropython/ports/esp32 && make -j3
	# cd $tmp_root_dir/micropython
	# make -C mpy-cross
	# cd ports/esp32 && make submodules && make
	# deactivate
}

build() {
	echo 'build'
	# update_system
	# clone_idf_from_gitee
	# clone_egt_from_gitee
	# update_idf_submodules
	# install_development_support
	# install_idf_toolchains
	clone_micropython_from_gitee
	update_micropython_submodules
	# compile_firmware
}

update() {
	update_egt

	update_idf
	update_idf_submodules

	update_micropython
	update_micropython_submodules
}

compile() {
	compile_firmware
}


if [[ $1 == "build" || $1 == "" ]]; then
	build
elif [[ $1 == "update" || $1 == "u" ]]; then
	update
elif [[ $1 == "compile" || $1 == "c" ]]; then
	compile
else
	echo "help"
fi
